//  A*
load_array %map C:\Users\abc\Desktop\map1.txt		//  
load_array %coord C:\Users\abc\Desktop\coord1.txt	//  

set $start A   //   A  
set $ending B  //   B  

// 0 -      , 1 -   
set #angle 0

set #logmap 1	 //    
set #readmap 1	//    
set #skipstep 0   // 0 -   , 1 - . ,      ,     .
set #click 1	   //  ,    .
set #send 0	   //  ,   Up, Down, Left, Right
set #hint 1	   //    
set #showpoint 0  //    (  )
set #wait 30	  //     Up, Down, Left, Right

   //    
set #handle workwindow
if  workwindow = 0
	Log   
	Log  
	stop_script
end_if
if  size(%map) = 0
	Log     .
	Log  
	stop_script
end_if
if  size(%coord) = 0
	Log     .
	Log  
	stop_script
end_if

log clear
log mode compact
set size(%tmp)
set size(%open)
set size(%close)
set size(%curr)
set size(%resultarray)
set #width size(%map[])
set #height size(%map)
set %sizemap indexof (%map (0))
set #sizemap size(%sizemap) + 1

//     
set %ending indexof (%map ($ending))
//     
set %start indexof (%map ($start))
//   A  B  
if  size(%ending) = 0 or size(%start) = 0
	if  size(%start) = 0
		log  A  
	else
		log  B  
	end_if
	log  
	end_script
end_if

// F,  G,  H,  _,   (X Y),   (X:Y),    (X Y)
init_arr %open (1) 0 0 0 0 %start [1 1] %start [1 2] %start [1 1]:%start [1 2] %start [1 1] %start [1 2]
while 1 = 1
	sort_array %open					//    F
	set #sizeclose size(%close) + 1
	init_arr %curr (1) %open [1 1] %open [1 2] %open [1 3] %open [1 4] %open [1 5] %open [1 6] %open [1 7] %open [1 8] %open [1 9]  //  
	init_arr %close (#sizeclose) %open [1 1] %open [1 2] %open [1 3] %open [1 4] %open [1 5] %open [1 6] %open [1 7] %open [1 8] %open [1 9]  //    
	if  #showpoint = 1
		left %coord[%curr[1 5] %curr[1 6]]
		wait 1
	end_if
	delete_array %open -1	 //    

	//    8   
	//  , 1
	set #x %curr [1 5] - 1
	set #y %curr [1 6]
	init_arr %tmp (1) #x : #y
	//     
	set %resultclose indexof (%close (%tmp[1]))
	if  #x > 0 and #y > 0 and #x <= #height and #y <= #width	 //       
		if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0   // (        )      
			set %resultopen indexof (%open (%tmp[1]))  //       ,    .
			if  size( %resultopen) = 0
				set #sizeopen size(%open) + 1
				set #g %curr [1 2] + 10				//  G
				set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10	  //  H
				set #f #g + #h																   //  F
				init_arr %open (#sizeopen) #f #g #h 1 #x #y %tmp[1] %curr [1 5] %curr [1 6]
			else
				//      
				set #tmp %resultopen [1 1]  //     
				set #g1 %open[#tmp 2]	   //  G     
				set #g %curr [1 2] + 10	 //  G   +        
				if  #g < #g1				//    
					//        
					//     
					set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
					set #f #g + #h
					init_arr %open (#tmp) #f #g #h 1 #x #y %tmp[1] %curr [1 5] %curr [1 6]
				end_if
			end_if
		end_if
	end_if
	//- 2
	if  #angle = 1				//     
		set #x %curr [1 5] - 1
		set #y %curr [1 6] + 1
		init_arr %tmp (1) #x : #y
		set %resultclose indexof (%close (%tmp[1]))
		if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
			set #tmp1 %curr[1 6]
			set #tmp2 %curr[1 5]
			if  %map [#x #tmp1] = 0 or %map[#tmp2 #y] = 0	 //     
				if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
					set %resultopen indexof (%open (%tmp[1]))
					if  size( %resultopen) = 0
						set #sizeopen size(%open) + 1
						set #g %curr [1 2] + 14
						set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
						set #f #g + #h
						init_arr %open (#sizeopen) #f #g #h 2 #x #y %tmp[1] %curr [1 5] %curr [1 6]
					else
						set #tmp %resultopen [1 1]
						set #g1  %open[#tmp 2]
						set #g %curr [1 2] + 10
						if  #g < #g1
							set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
							set #f #g + #h
							init_arr %open (#tmp) #f #g #h 2 #x #y %tmp[1] %curr [1 5] %curr [1 6]
						end_if
					end_if
				end_if
			end_if
		end_if
	end_if
	// 3
	set #x %curr [1 5]
	set #y %curr [1 6] + 1
	init_arr %tmp (1) #x : #y
	set %resultclose indexof (%close (%tmp[1]))
	if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
		if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
			set %resultopen indexof (%open (%tmp[1]))
			if  size( %resultopen) = 0
				set #sizeopen size(%open) + 1
				set #g %curr [1 2] + 10
				set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
				set #f #g + #h
				init_arr %open (#sizeopen) #f #g #h 3 #x #y %tmp[1] %curr [1 5] %curr [1 6]
			else
				set #tmp %resultopen [1 1]
				set #g1  %open[#tmp 2]
				set #g %curr [1 2] + 10
				if  #g < #g1
					set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
					set #f #g + #h
					init_arr %open (#tmp) #f #g #h 3 #x #y %tmp[1] %curr [1 5] %curr [1 6]
				end_if
			end_if
		end_if
	end_if
	//	- 4
	if  #angle = 1
		set #x %curr [1 5] + 1
		set #y %curr [1 6] + 1
		init_arr %tmp (1) #x : #y
		set %resultclose indexof (%close (%tmp[1]))
		if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
			set #tmp1 %curr[1 6]
			set #tmp2 %curr[1 5]
			if  %map [#x #tmp1] = 0 or %map[#tmp2 #y] = 0
				if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
					set %resultopen indexof (%open (%tmp[1]))
					if  size( %resultopen) = 0
						set #sizeopen size(%open) + 1
						set #g %curr [1 2] + 14
						set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
						set #f #g + #h
						init_arr %open (#sizeopen) #f #g #h 4 #x #y %tmp[1] %curr [1 5] %curr [1 6]
					else
						set #tmp %resultopen [1 1]
						set #g1  %open[#tmp 2]
						set #g %curr [1 2] + 10
						if  #g < #g1
							set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
							set #f #g + #h
							init_arr %open (#tmp) #f #g #h 4 #x #y %tmp[1] %curr [1 5] %curr [1 6]
						end_if
					end_if
				end_if
			end_if
		end_if
	end_if
	// 5
	set #x %curr [1 5] + 1
	set #y %curr [1 6]
	init_arr %tmp (1) #x : #y
	set %resultclose indexof (%close (%tmp[1]))
	if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
		if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
			set %resultopen indexof (%open (%tmp[1]))
			if  size( %resultopen) = 0
				set #sizeopen size(%open) + 1
				set #g %curr [1 2] + 10
				set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
				set #f #g + #h
				init_arr %open (#sizeopen) #f #g #h 5 #x #y %tmp[1] %curr [1 5] %curr [1 6]
			else
				set #tmp %resultopen [1 1]
				set #g1  %open[#tmp 2]
				set #g %curr [1 2] + 10
				if  #g < #g1
					set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
					set #f #g + #h
					init_arr %open (#tmp) #f #g #h 5 #x #y %tmp[1] %curr [1 5] %curr [1 6]
				end_if
			end_if
		end_if
	end_if
	//- 6
	if  #angle = 1
		set #x %curr [1 5] + 1
		set #y %curr [1 6] - 1
		init_arr %tmp (1) #x : #y
		set %resultclose indexof (%close (%tmp[1]))
		if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
			set #tmp1 %curr[1 6]
			set #tmp2 %curr[1 5]
			if  %map [#x #tmp1] = 0 or %map[#tmp2 #y] = 0
				if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
					set %resultopen indexof (%open (%tmp[1]))
					if  size( %resultopen) = 0
						set #sizeopen size(%open) + 1
						set #g %curr [1 2] + 14
						set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
						set #f #g + #h
						init_arr %open (#sizeopen) #f #g #h 6 #x #y %tmp[1] %curr [1 5] %curr [1 6]
					else
						set #tmp %resultopen [1 1]
						set #g1  %open[#tmp 2]
						set #g %curr [1 2] + 10
						if  #g < #g1
							set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
							set #f #g + #h
							init_arr %open (#tmp) #f #g #h 6 #x #y %tmp[1] %curr [1 5] %curr [1 6]
						end_if
					end_if
				end_if
			end_if
		end_if
	end_if
	// 7
	set #x %curr [1 5]
	set #y %curr [1 6] - 1
	init_arr %tmp (1) #x : #y
	set %resultclose indexof (%close (%tmp[1]))
	if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
		if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
			set %resultopen indexof (%open (%tmp[1]))
			if  size( %resultopen) = 0
				set #sizeopen size(%open) + 1
				set #g %curr [1 2] + 10
				set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
				set #f #g + #h
				init_arr %open (#sizeopen) #f #g #h 7 #x #y %tmp[1] %curr [1 5] %curr [1 6]
			else
				set #tmp %resultopen [1 1]
				set #g1  %open[#tmp 2]   //  4 - 3 G
				set #g %curr [1 2] + 10  //   4 2-4 3
				if  #g < #g1
					set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
					set #f #g + #h
					init_arr %open (#tmp) #f #g #h 7 #x #y %tmp[1] %curr [1 5] %curr [1 6]
				end_if
			end_if
		end_if
	end_if

	//- 8
	if  #angle = 1
		set #x %curr [1 5] - 1
		set #y %curr [1 6] - 1
		init_arr %tmp (1) #x : #y
		set %resultclose indexof (%close (%tmp[1]))
		if  #x > 0 and #y > 0 and #x <= #height and #y <= #width
			set #tmp1 %curr[1 6]
			set #tmp2 %curr[1 5]
			if  %map [#x #tmp1] = 0 or %map[#tmp2 #y] = 0
				if  (%map [#x #y] = 0 or %map [#x #y] = $ending) and size(%resultclose) = 0
					set %resultopen indexof (%open (%tmp[1]))
					if  size( %resultopen) = 0
						set #sizeopen size(%open) + 1
						set #g %curr [1 2] + 14
						set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
						set #f #g + #h
						init_arr %open (#sizeopen) #f #g #h 8 #x #y %tmp[1] %curr [1 5] %curr [1 6]
					else
						set #tmp %resultopen [1 1]
						set #g1  %open[#tmp 2]
						set #g %curr [1 2] + 10
						if  #g < #g1
							set #h (abs(eval(#x - %ending [1 1])) + abs(eval(#y - %ending [1 2]))) * 10
							set #f #g + #h
							init_arr %open (#tmp) #f #g #h 8 #x #y %tmp[1] %curr [1 5] %curr [1 6]
						end_if
					end_if
				end_if
			end_if
		end_if
	end_if
	//           
	if  (%curr [1 5] = %ending [1 1] and %curr [1 6] = %ending [1 2]) or size(%open) = 0
		if  %curr [1 5] != %ending [1 1] and %curr [1 6] != %ending [1 2]
			log   
			end_script
		end_if

		//      B   A      
		set size(%tmp)
		init_arr %tmp (1) %close [size(%close) 8] %close [size(%close) 9]
		while %tmp [size(%tmp) 1] != %start [1 1] or %tmp [size(%tmp) 2] != %start [1 2]
			set %pathAB indexof (%close (%tmp [size(%tmp) 1]:%tmp [size(%tmp) 2]))
			set #zz size(%tmp) + 1
			init_arr %tmp (#zz) %close [%pathAB [1 1] 8] %close [%pathAB [1 1] 9] %close [%pathAB [1 1] 4]
		end_while
		set #z 0
		set delimiter ' '
		for #i size(%tmp) 1 -1
			set #z #z + 1
			init_arr %resultarray (#z) %tmp [#i]
		end_for
		set delimiter
		// save_array %open C:\Users\abc\Desktop\open.txt	  //  
		// save_array %close C:\Users\abc\Desktop\close.txt	//  
		// save_array %resultarray C:\Users\abc\Desktop\resultarray.txt   //         %map
		log    : timer 

		//  
		if  #logmap = 1
			gosub logmap
		end_if

		//    
		if  #readmap = 1
			gosub readmap
		end_if

		//  
		if  #click = 1
			gosub hod
		end_if
		end_script
	end_if
	if  #hint = 1
		hint  size(%close)  #sizemap .
	end_if
end_while
end_script

:logmap
	for #i 1 size(%map)
		log %map [#i]
	end_for
return

:readmap
	set workwindow #handle
	wait 500
	showwindow #handle
	wait 500

	set #tmpi size(%resultarray) - 1
	for #i 1 size(%resultarray)
		if  (#skipstep = 0) or (mod (#i 2) = 0 or #tmpi = #i)
			switch %resultarray [#i 3]
				case 1: log 
					if  #send = 1
						send {Up}
						wait #wait
					end_if
					break
				case 2: log -
					if  #send = 1
						send {Up}
						wait #wait
						send {Right}
						wait #wait
					end_if
					break
				case 3: log 
					if  #send = 1
						send {Right}
						wait #wait
					end_if
					break
				case 4: log -
					if  #send = 1
						send {Down}
						wait #wait
						send {Right}
						wait #wait
					end_if
					break
				case 5: log 
					if  #send = 1
						send {Down}
						wait #wait
					end_if
					break
				case 6: log -
					if  #send = 1
						send {Down}
						wait #wait
						send {Left}
						wait #wait
					end_if
					break
				case 7: log 
					if  #send = 1
						send {Left}
						wait #wait
					end_if
					break
				case 8: log -
					if  #send = 1
						send {Up}
						wait #wait
						send {Left}
						wait #wait
					end_if
					break
			end_switch
		end_if
	end_for
return
:hod
	if  #showpoint = 1
		alarm
		set showscriptprocessing 1
		log  .   .
		log       .
		pause_script
		set showscriptprocessing 0
	end_if
	showwindow #handle
	wait 500
	if  #skipstep = 0
		for #i 2 size(%resultarray)	 //   
			kleft %coord[%resultarray [#i 1] %resultarray [#i 2]]
			// log  %coord[%resultarray [#i 1] %resultarray [#i 2]]
			wait 1
		end_for
	else
		for #i 3 size(%resultarray) 2   //    
			kleft %coord[%resultarray [#i 1] %resultarray [#i 2]]
			// log  %coord[%resultarray [#i 1] %resultarray [#i 2]]
			wait 1
		end_for
	end_if
return